#!/usr/bin/perl
#target_mod.cgi
require './tgtman-lib.pl';
use CGI;
&ReadParse();

&header($text{'iscsiipdeny_header'}, "", "ipdeny", 0, 1);
$in = new CGI();

$tname = $in{'targetid'};

if(!$tname) {
	$tname = $in->param('tgtname');
}

@tlist=&get_target_list();
$found = 0;
my $tg;
foreach $t (@tlist) {
	if($t->{'fullname'} eq $tname) {
		$found = 1;
		$tg = $t;
	}
}
if(!$found){
	print "no target selected";
}
print "<form name=ipset  method=post>\n";
print "<table class=mode1>\n";
print "<tr><td width=120><b>$text{'target_name'}</b></td><td>$tg->{'gstr'}:<b>$tg->{'name'}</b></td></tr>\n";
print "<tr> </tr>";
$targetfullname = $tg->{'gstr'}.":".$tg->{'name'};
#&list_iprule($targetfullname);
#--------------------------------
		$target = $targetfullname;
		my ($rulelineallow, $rulelinedeny, @allowip, @denyip);
    open(IA, "/etc/initiators.allow");
    my @allowfile=<IA>;
    close(IA);

    open(MT, "/etc/initiators.deny");
    my @denyfile=<MT>;
    close(MT);
    
		foreach (@allowfile) {
			$_ =~ s/\r|\n//g;
			if ($_ =~ /^$target\s+(\S+)$/) {
				$rulelineallow = $1;				
			}
		}

		if($rulelineallow) {
			@allowip = split(/,/, $rulelineallow)
		}

		foreach (@denyfile) {
			$_ =~ s/\r|\n//g;
			if ($_ =~ /^$target\s+(\S+)$/) {
				$rulelinedeny = $1;
			}
		}
		
		if($rulelinedeny) {
			@denyip = split(/,/, $rulelinedeny)
		}
				
    my $mlines=@allow;
    print "<table width=800 class=mode2><tr>\n";
    print "<td  valign=top><div class=main><table  class=mode1 border=0 bordercolor=#33ccff cellspacing=1 cellpadding=1 width=100%>\n";
    print "<tr class=title1> <th align=left width=400>$text{'ipaddr'}</th> ",
          "<th align=left width=200>$text{'filtertype'}</th>";
          #"<th align=left>$text{'modcomment'}</th> </tr>\n";
    $i = 0;
    foreach(@allowip){
    	$i++;
    	$str = $_.":A";
			print "<tr class=mode1><td><input type=radio name=ruleline value=\"$str\">$_</td><td>$text{'allow'}</td></tr>";
		}
		foreach(@denyip){
			$i++;
			$str = $_.":D";
			print "<tr class=mode1><td><input type=radio name=ruleline value=\"$str\">$_</td><td>$text{'deny'}</td></tr>";
		}
    print "</table></div></td>\n";

    print "<td valign=top width=160><table class=mode1 border=0 cellspacing=8 cellpadding=1 width=100%>\n";
    printf "<tr><td align=center><input type=submit name=mcreate value=\"$text{'ruleadd'}\" onclick=\'return on_add_click();\' class=button></td></tr>\n";
    print "<tr><td align=center><input type=submit name=mdelete value=\"$text{'ruledelete'}\" onclick=\'return on_delete_click($i, \"$text{'confirm_delrule'}\", \"$text{'chooseone'}\");\' class=button></td></tr>\n";
		print "<tr><td align=center><input type=submit name=mdelete value=\"$text{'ruledelall'}\" onclick=\'return on_deleteall_click($i, \"$text{'confirm_delallrule'}\");\' class=button></td></tr>\n";

    print "</table></td></tr>";
    #print "<tr><td colspan=2><i>$text{'hint'}</i></td></tr>";
    print "</table>\n";  
#--------------------------------
#print "<tr><td></td><td><input name=actmod type=submit class=button1 value=$text{'submit_modify'} onclick='return on_click();' disabled=true></td></tr>\n";
print "</table>\n";

print "<p><input name=command type=hidden value=\'\'>\n";
print "<p><input name=tgtname type=hidden value=$targetfullname>\n";
print "</form>\n";
&footer("index.cgi",$text{'index'});


print <<EOF;
<script>

function on_add_click()
{
    document.ipset.action = 'ruleadd.cgi';
    //document.form[0].command.value = 'ruleadd'; 
}

function on_delete_click(length, confirmdel, chooseone)
{		
    var i;
    var find = 0;
    if(length < 2)
    {
          if(length < 1)
          {
              return false;
          }
          else
          {
              if(document.ipset.ruleline.checked)
              {
              		 //alert(document.ipset.ruleline.value);
                   find = 1;
              }
          }
    }
    else
    {
          for(i = 0; i < length; i++)
          {
              if(document.ipset.ruleline[i].checked)
              {
              		 //alert(document.ipset.ruleline[i].value);	
                   find = 1;
              }

          }
    }
    if(find == 0)
    {
          alert(chooseone);
          return false;
    }
    if(!confirm(confirmdel))
    {
       return false;
    }    

    document.ipset.command.value = 'delete';
    document.ipset.action = 'delrule.cgi';
}

function on_deleteall_click(length, confirmdel)
{

    if(!confirm(confirmdel))
    {
       return false;
    }    

    document.ipset.command.value = 'delall';
    document.ipset.action = 'delrule.cgi';
}
</script>
EOF
